package com.mofang.widget;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.TextView;

import com.mofang.R;

public class AlertDialog extends Dialog {

	public AlertDialog(Context context, boolean cancelable,
			OnCancelListener cancelListener) {
		super(context, cancelable, cancelListener);
	}

	public AlertDialog(Context context, int theme) {
		super(context, theme);
	}

	public AlertDialog(Context context) {
		super(context);
	}

	public static class Builder {
		private Context context;
		private String message;
		private View contentView;
		private int imageResourceId;

		public void setImageResourceId(int imageResourceId) {
			this.imageResourceId = imageResourceId;
		}

		public Builder(Context context) {
			this.context = context;
		}

		public Builder setMessage(String message) {
			this.message = message;
			return this;
		}

		/**
		 * Set the Dialog message from resource
		 * 
		 * @param title
		 * @return
		 */
		public Builder setMessage(int message) {
			this.message = (String) context.getText(message);
			return this;
		}

		public Builder setContentView(View v) {
			this.contentView = v;
			return this;
		}

		public AlertDialog create() {
			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			// instantiate the dialog with the custom Theme
			final AlertDialog dialog = new AlertDialog(context, R.style.Dialog);
			View layout = inflater.inflate(R.layout.dialog_alert_layout, null);
			dialog.addContentView(layout, new LayoutParams(
					LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
			// set the dialog title
			((TextView) layout.findViewById(R.id.txt_dialog_alert_txt))
					.setText(message);
			((ImageView) layout.findViewById(R.id.iv_dialog_alert_pic))
					.setBackgroundResource(imageResourceId);
			dialog.setContentView(layout);

			// Window window = dialog.getWindow();
			// WindowManager.LayoutParams lp = window.getAttributes();
			// // 设置透明度为0.3
			// lp.alpha = 0.9f;
			// window.setAttributes(lp);
			// builder.create().show();

			return dialog;
		}

	}
}
